package timetable.student;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class ExpandableListAdapter extends BaseExpandableListAdapter {

    @Override
    public boolean areAllItemsEnabled()
    {
        return true;
    }

    private Context context;

    private ArrayList<String> groups = new ArrayList<String>();
    
    private ArrayList<ArrayList<Tdata>> children;

    public ExpandableListAdapter(Context context, 
            ArrayList<ArrayList<Tdata>> children) {
        this.context = context;
        this.children = children;
        groups.add("Monday");
        groups.add("Tuesday");
        groups.add("Wednesday");
        groups.add("Thursday");
        groups.add("Friday");
        for(int i =0;i<5;++i)
        {
        	children.add(new ArrayList<Tdata>());
        }
    }

    public void clear()
    {
    	children.clear();
    	for(int i =0;i<5;++i)
        {
        	children.add(new ArrayList<Tdata>());
        }
    }
    
    public void addItem(Tdata tdata) {
        int index = groups.indexOf(tdata.getDay());
        this.children.get(index).add(tdata);
        
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return children.get(groupPosition).get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }
    
    
    // Return a child view. You can load your custom layout here.
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
            View convertView, ViewGroup parent) {
    	if(getChildrenCount(groupPosition) > 0)
    	{
        Tdata tdata = (Tdata) getChild(groupPosition, childPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.childlayout ,null);
        }
        TextView tv = (TextView) convertView.findViewById(R.id.tvChild);
        tv.setText(tdata.getStartTime()+": "+ tdata.getModule());
    	}
    	else
    	{
    		
      		if (convertView == null) {
                LayoutInflater infalInflater = (LayoutInflater) context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = infalInflater.inflate(R.layout.childlayout ,null);
            }
            TextView tv = (TextView) convertView.findViewById(R.id.tvChild);
            tv.setText("No Classes Set For Today");
    	}
        
        return convertView;
    }
    
    @Override
    public int getChildrenCount(int groupPosition) {
    	 return children.get(groupPosition).size();
    	
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groups.get(groupPosition);
    }

    @Override
    public int getGroupCount() {  
        return groups.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    // Return a group view. You can load your custom layout here.
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
            ViewGroup parent) {
        String group = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.grouplayout, null);
        }
        TextView tv = (TextView) convertView.findViewById(R.id.tvGroup);
        tv.setText(group);
        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public boolean isChildSelectable(int arg0, int arg1) {
        return true;
    }

}
